/**
 *
 */

package com.person.modules.person.controller;

import com.person.common.annotation.SysLog;
import com.person.common.utils.DateUtils;
import com.person.common.utils.PageUtils;
import com.person.common.utils.R;
import com.person.common.validator.ValidatorUtils;
import com.person.modules.person.entity.InterviewPlanEntity;
import com.person.modules.person.service.InterviewPlanService;
import com.person.modules.sys.controller.AbstractController;
import com.person.modules.sys.entity.SysUserEntity;
import com.person.modules.sys.service.SysUserService;
import com.person.modules.sys.shiro.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 面试管理
 *
 * @author
 */
@RestController
@RequestMapping("/person/interview")
public class InterviewPlanController extends AbstractController {
    @Autowired
    private InterviewPlanService interviewPlanService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private JavaMailSenderImpl mailSender;

    /**
     * 所有面试列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("person:apply:list")
    public R list(@RequestParam Map<String, Object> params) {

        if (getUserId()==1||getUserId()==4) {
            PageUtils page = interviewPlanService.queryPage(params);
            return R.ok().put("page", page);
        } else {

            PageUtils page = interviewPlanService.selectName(params);
            return R.ok().put("page", page);
        }
    }


    /**
     * 面试信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("person:apply:info")
    @ResponseBody
    public R info(@PathVariable("id") Long id) {
        InterviewPlanEntity interview = interviewPlanService.getById(id);

        return R.ok().put("interview", interview);
    }

    /**
     * 新增面试
     */
    @SysLog("新增面试")
    @RequestMapping("/save")
    @RequiresPermissions("person:apply:save")
    public R save(@RequestBody InterviewPlanEntity apply) throws MessagingException {
        ValidatorUtils.validateEntity(apply);
        apply.setCreateTime(DateUtils.currentTimeFormat());


        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("邀请您作为面试官通知");
        helper.setFrom("1203313263@qq.com");
        helper.setTo(sysUserService.getById(apply.getMeetUserId()).getEmail());
        helper.setText("您好，" + sysUserService.getById(apply.getMeetUserId()).getName() + "，您作为本次面试的面试官，具体时间请在系统中查看");
        helper.addAttachment("1.jpg", new File("C:\\Users\\Lxx\\Desktop\\壁纸\\1.jpg"));
        mailSender.send(mimeMessage);

        MimeMessage mimeMessage1 = mailSender.createMimeMessage();
        MimeMessageHelper helper1 = new MimeMessageHelper(mimeMessage1, true);
        helper1.setSubject("邀请您作为面试通知");
        helper1.setFrom("1203313263@qq.com");
        helper1.setTo(apply.getEmail());
        helper1.setText("您好，" + apply.getCandidate() + "，xxx有限公司邀请您本次面试，具体时间为"+apply.getMeetTime()+"如有问题，请咨询电话"+sysUserService.getById(apply.getMeetUserId()).getMobile());
        helper1.addAttachment("1.jpg", new File("C:\\Users\\Lxx\\Desktop\\壁纸\\1.jpg"));
        mailSender.send(mimeMessage1);
        interviewPlanService.save(apply);

        return R.ok();
    }

    /**
     * 更新面试结果面试
     */
    @SysLog("跟新面试结果")
    @RequestMapping("/update")
    @RequiresPermissions("person:apply:update")
    public R update(@RequestBody InterviewPlanEntity apply) throws MessagingException {
        ValidatorUtils.validateEntity(apply);

      //  System.out.println(apply.getStatus());
        if (apply.getStatus() == 1) {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            helper.setSubject("美团优选欢迎您加入");
            helper.setFrom("1203313263@qq.com");
            helper.setTo(apply.getEmail());
            helper.setText("您好，" + apply.getCandidate() + "同学，恭喜您通过本次面试，稍后人事会联系您，详情见附录");
            helper.addAttachment("1.jpg", new File("C:\\Users\\Lxx\\Desktop\\壁纸\\1.jpg"));
            mailSender.send(mimeMessage);
        }
        interviewPlanService.update(apply);

        return R.ok();
    }

    /**
     * 删除面试
     */
    @SysLog("删除面试")
    @RequestMapping("/delete")
    @RequiresPermissions("person:apply:delete")
    public R delete(@RequestBody Long[] ids) {
        interviewPlanService.deleteBatch(ids);
        return R.ok();
    }
}
